<?php
class Sso_UserController extends Ext_Controller_Action
{
	public function preDispatch()
	{
		parent::preDispatch();
		if ( Zend_Controller_Action_HelperBroker::hasHelper( 'layout' ) ) {
            $this->_helper->layout->disableLayout();
        }
        $this->_helper->viewRenderer->setNoRender();
	}
    public function externalloginAction()
    {
        if( $this->getRequest()->token ) {
            $user = new Ext_Model_User();
            $loginStatus = new SsoWS_Model_UserLoginStatus(); 
            $row = $loginStatus->getRowByToken( $this->getRequest()->token );
            if( $row->token == $this->getRequest()->token &&  $row->is_login == 'T' ) {
                setcookie(
                    Zend_Registry::get('config')->cookie->token->name,
                    $row->token,
                    $loginStatus->getTimeLife(),
                    '/',
                    Zend_Registry::get('config')->site->domain,
                    false
                );
                if( $this->getRequest()->url_response ) {
                	if( strpos($this->getRequest()->url_response, 'token')) {
                    	$url = $this->getRequest()->url_response;
                	}else {
                		$url = $this->getRequest()->url_response . 'token=' .$row->token;
                	}
                    return $this->_redirect( $url );
                }
            }
        }
        return $this->_redirect( $url );
    }

    public function checkAction()
    {
        $url = ( isset( $_GET['url_response'] ))?$_GET['url_response']:'/';
        $loginStatus = new SsoWS_Model_UserLoginStatus();
        if( isset( $_COOKIE[ Zend_Registry::get('config')->cookie->token->name ] ) ){
            $token = $_COOKIE[ Zend_Registry::get('config')->cookie->token->name ];
            if( $loginStatus->getStatus( $token ) == 'T' ) {
				if( ! strpos($this->getRequest()->url_response, '?')) {
                	$url .= "?token=" . $token;
                }else {
                	$url .= "&token=" . $token;
                }
                return $this->_redirect( $url );
            }
        }
    	if( ! strpos($this->getRequest()->url_response, 'token')) {
          	$url .= "?token=0";
		}else {
			$url .= "&token=" . $token;
		}
        return $this->_redirect(  $url );
	}
}